
using Abp.Domain.Repositories;
using Abp.EntityFrameworkCore.Repositories;
using Abp.Timing;
using Microsoft.EntityFrameworkCore;
using SOEI.Solution.FileUpLoader.Configuration;
using SOEI.Solution.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;


namespace SOEI.Solution.FileUpLoader.DomainService
{
	/// <summary>
	/// 领域服务层一个模块的核心业务逻辑
	///</summary>
	public class AttachManager :DomainServiceBase, IAttachManager
	{
		
		private readonly IRepository<Attach,long> _attachRepository;

		private readonly UpLoaderConfiguration _configuration;

		/// <summary>
		/// 根据当前日期生成目录名称
		/// </summary>
		//public string GetDirectoryName {
		//	get {
		//		var root = _configuration.RootPath;
		//		var p1 = Clock.Now.ToString("yyyyMM");
		//		var p2 = Clock.Now.ToString("dd");
		//		var path = Path.Combine(root, p1, p2);
		//		return path;
		//	}
		//}

		/// <summary>
		/// 【Attach】的构造方法
		/// 通过构造函数注册服务到依赖注入容器中
		///</summary>
		public AttachManager(
			IRepository<Attach, long> attachRepository,
			UpLoaderConfiguration configuration
		)	{
			_attachRepository =  attachRepository;
			_configuration = configuration;

			//this.InitialStructure();
		}

		#region -------------------------------------------------辅助工具生成---------------------------------------------- 

		/// <summary>
		/// 返回列表查询用
		/// </summary>
		/// <returns></returns>
		public IQueryable<Attach> QueryEntityListAsNoTracking() { 

			var query = _attachRepository.GetAll().AsNoTracking()
						.Select(x => new Attach
						{
						   
							FileName = x.FileName,
							Path = x.Path,
							Size = x.Size,
							Extension = x.Extension,
							ContentType = x.ContentType,
							ModuleID = x.ModuleID,
						});
			return query;
		}

		/// <summary>
		/// 【Attach】返回表达式数的实体信息即IQueryable类型
		/// </summary>
		/// <returns>IQueryable</returns>
		public IQueryable<Attach> Query()
		{
			return _attachRepository.GetAll();
		}
		/// <summary>
		/// 【Attach】返回即IQueryable类型的实体，不包含EF Core跟踪标记
		/// </summary>
		/// <returns>IQueryable</returns>
		public IQueryable<Attach> QueryAsNoTracking()
		{
			return _attachRepository.GetAll().AsNoTracking();
		}
		/// <summary>
		/// 【Attach】根据Id查询实体信息
		/// </summary>
		/// <param name="id"></param>
		/// <returns>实体</returns>
		public async Task<Attach> FindByIdAsync(long id)
		{
			var entity = await _attachRepository.GetAsync(id);
			return entity;
		}
		/// <summary>
		/// 【Attach】检查实体是否存在
		/// </summary>
		/// <param name="id"></param>
		/// <returns>bool</returns>
		public async Task<bool> IsExistAsync(long id)
		{
			var result = await _attachRepository.GetAll().AnyAsync(a => a.Id == id);
			return result;
		}
		/// <summary>
		/// 【Attach】创建实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<Attach> CreateAsync(Attach entity)
		{
			entity.OrderNum = 1 + await this._attachRepository.CountAsync(x => x.ModuleID == entity.ModuleID);
			entity.Id = await _attachRepository.InsertAndGetIdAsync(entity);
			return entity;
		}
		/// <summary>
		/// 【Attach】更新实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task UpdateAsync(Attach entity)
		{
			await _attachRepository.UpdateAsync(entity);
		}
		/// <summary>
		/// 【Attach】删除实体
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task DeleteAsync(long id)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _attachRepository.DeleteAsync(id);
		}
		/// <summary>
		/// 【Attach】批量删除
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task BatchDelete(List<long> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _attachRepository.DeleteAsync(a => input.Contains(a.Id));
		}
		#endregion


		#region -------------------------------------------------用户自定义------------------------------------------------
		/*请在此扩展领域服务接口*/
		#endregion

		#region -------------------------------------------------辅助方法------------------------------------------------

		/// <summary>
		/// 获取指定模块下的附件清单
		/// </summary>
		/// <param name="ModuleID"></param>
		/// <returns></returns>
		public async Task<ICollection<Attach>> GetAttachByModuleID(Guid ModuleID)
		{
			return await this.QueryAsNoTracking().Where(x => x.ModuleID == ModuleID).ToListAsync();
		}

		/// <summary>
		/// 获取新的文件名称
		/// </summary>
		/// <param name="ExtName">原始扩展名称</param>
		/// <returns></returns>
		public string GetNewFileName(string ExtName)
		{
			var p1 = Clock.Now.ToString("yyyyMMddHHmmssms");
			return string.Format("{0}{1}", p1, ExtName);
		}

		#endregion







	}
}
